package com.example.demo.controller;


import com.example.demo.entity.*;
import com.example.demo.service.*;
import com.example.demo.vo.DataView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping(value = "/receiveInfo")
public class AllInfoReceiveController {

    @Value("${qrCode.voices}")
    private String voicePath;

    @Value("${qrCode.videos}")
    private String videoPath;

    @Value("${qrCode.images}")
    private String imagesPath;

    @Autowired
    private ContentCcService contentCcService;

    @Autowired
    private PeopleCcService peopleCcService;

    @Autowired
    private VoiceCcService voiceCcService;

    @Autowired
    private PictureCcService pictureCcService;

    @Autowired
    private VideoCcService videoCcService;

    @RequestMapping(value = "/receiveContent")
    public DataView receiveContent(@RequestParam(value = "content")String content, HttpServletRequest request)
    {

        DataView dataView = new DataView();
        String qrCode = request.getHeader("qrCode");
        if(null == qrCode)
        {
            dataView.setCode(400);
            dataView.setMsg("请扫描二维码");
            return dataView;
        }
        if(content == null)
        {
            dataView.setCode(300);
            dataView.setMsg("没有内容");
            return dataView;
        }

        ContentCc contentCc = new ContentCc();
        contentCc.setContent(content);
        contentCc.setQrCodeId(qrCode);
        contentCcService.save(contentCc);
        dataView.setCode(200);
        dataView.setMsg("内容保存成功");
        return dataView;
    }


    @RequestMapping(value = "/receiveVoice")
    public DataView receiveVoice(@RequestParam(value = "/voiceFile") MultipartFile voiceFile,HttpServletRequest request) throws IOException {
        DataView dataView = new DataView();
        String qrCode = request.getHeader("qrCode");
        if(null == qrCode)
        {
            dataView.setCode(400);
            dataView.setMsg("请扫描二维码");
            return dataView;
        }
        if(null == voiceFile)
        {
            dataView.setCode(300);
            dataView.setMsg("录音内容为空");
            return dataView;
        }

        String originalFilename = voiceFile.getOriginalFilename();
        String path = voicePath+qrCode+"\\"+originalFilename;

        File file = new File(path);

        if(!file.exists())
        {
            file.mkdirs();
        }
        voiceFile.transferTo(file);
        VoiceCc voiceCc = new VoiceCc();
        voiceCc.setVoicePath(path);
        voiceCc.setWechatOpenid(qrCode);
        voiceCcService.save(voiceCc);
        dataView.setCode(200);
        dataView.setMsg("录音保存成功");
        return dataView;
    }


    @RequestMapping(value = "/receiveToPeopleName")
    private DataView receiveToPeopleName(@RequestParam(value = "peopleName") String toPeopleName,HttpServletRequest request)
    {
        DataView dataView = new DataView();

        String qrCode = request.getHeader("qrCode");

        if(null == qrCode)
        {
            dataView.setCode(400);
            dataView.setMsg("请扫描二维码");
            return dataView;
        }

        if(null == toPeopleName)
        {
            dataView.setCode(300);
            dataView.setMsg("人名为空");
            return dataView;
        }

        PeopleNameCc peopleNameCc = new PeopleNameCc();
        peopleNameCc.setPeopleName(toPeopleName);
        peopleNameCc.setQrCodeId(qrCode);
        peopleCcService.save(peopleNameCc);
        dataView.setCode(200);
        dataView.setMsg("人名保存成功");
        return dataView;
    }


    /**
     * 接收图片
     */
    @PostMapping(value = "/receivesPictures")
    public DataView receivesQRCodePictures(@RequestParam(value = "images")MultipartFile []images, HttpServletRequest request) throws IOException {
        DataView dataView = new DataView();
        String qrCode = request.getHeader("qrCode");
        if(qrCode == null)
        {
            dataView.setCode(300);
            dataView.setMsg("请扫描二维码");
            return dataView;
        }
        for (MultipartFile multipartFile:images) {

            String originalFilename = multipartFile.getOriginalFilename();
            String path = imagesPath+qrCode+"\\"+originalFilename;
            File file = new File(path);
            if(!file.exists())
            {
                file.mkdirs();
            }
            multipartFile.transferTo(file);
            PictureCc pictureCc = new PictureCc();
            pictureCc.setPicturePath(path);
            pictureCc.setQrCodeId(qrCode);
            pictureCcService.save(pictureCc);
        }
        dataView.setCode(200);
        dataView.setMsg("图片上传成功");
        return dataView;
    }

    /**
     * 接收视频
     */
    @RequestMapping(value = "/receiveVideos")
    public DataView receiveVideos(@RequestParam(value = "files") MultipartFile []videoFiles,HttpServletRequest request) throws IOException {
        DataView dataView = new DataView();
        String qrCode = request.getHeader("qrCode");
        if(null == qrCode)
        {
            dataView.setCode(300);
            dataView.setMsg("扫描二维码");
            return dataView;
        }
        if(videoFiles == null)
        {
            dataView.setCode(200);
            dataView.setMsg("");
            return dataView;
        }

        for(MultipartFile multipartFile : videoFiles)
        {
            String originalFilename = multipartFile.getOriginalFilename();

            String path = videoPath +qrCode+"\\"+originalFilename;

            File file = new File(path);

            if(!file.exists())
            {
                file.mkdirs();
            }
            multipartFile.transferTo(file);
            VideoCc videoCc = new VideoCc();
            videoCc.setVideoPath(path);
            videoCc.setQrCodeId(qrCode);
            videoCcService.save(videoCc);
        }
        dataView.setCode(200);
        dataView.setMsg("视频上传成功");
        return dataView;
    }

}
